<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>留言板</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        li {
            list-style: none;
        }

        a {
            color: inherit;
            text-decoration: none;
        }

        .commentBar {
            width: 600px;
            margin: 0 auto;
        }

        .commentBar .commentSendBox {
            margin-top: 50px;
        }

        .commentBar .commentSendBox textarea {
            width: 100%;
            height: 200px;
            resize: none;
            padding: 10px;
            box-sizing: border-box;
            outline: none;
        }

        .commentBar .commentSendBox .sendBox {
            display: flex;
            justify-content: space-between;
        }

        .commentListBox {
            margin-top: 50px;
            font-size: 12px;
            line-height: 18px;
        }

        .showComment li {
            margin-top: 20px;
            padding: 10px;
            border: 1px dashed #333;
        }
        .showComment li .nick {
            float: left;
            font-size: 20px;
            color: cornflowerblue;
        }
        .showComment li .time {
            float: right;
            font-size: 12px;
            color: coral;
        }
        .showComment li .comment {
            clear: both;
            margin-top: 10px;
            
        }

    </style>
</head>

<body>
    <div class="commentBar">
        <div class="commentSendBox">
            <textarea name="" id="" cols="30" rows="10" class="commentCon"></textarea>
            <div class="sendBox">
                <div class="commentTips">您还可以输入50个字</div>
                <button class="commentSend">发送</button>
            </div>
        </div>
        <div class="showComment">
            <!-- <li>
                <div class="nick">留言人：a123123</div>
                <div class="time">2021-9-4 18:20</div>
                <div class="comment">1213121</div>
            </li> -->
        </div>
    </div>
</body>
<script>
    var commentCon = document.getElementsByClassName("commentCon")[0];
    var commentSend = document.getElementsByClassName("commentSend")[0];
    var commentTips = document.getElementsByClassName("commentTips")[0];
    var showComment = document.getElementsByClassName("showComment")[0];

    commentCon.onkeypress = commentCon.onkeyup = inputHandler;
    commentSend.onclick = enterHandler;

    // 输入框的方法
    function inputHandler(e) {
        var e = e || window.event;
        var comment = commentCon.value;
        var maxChar = 50; //规定能输入的字数
        var num = maxChar - comment.length;
        commentTips.textContent = `您还可以输入${num}个字`;
        if (num <= 0) {
            // 如果num=0，阻止浏览器的默认行为(阻止输入)
            if (e.preventDefault) {
                e.preventDefault();
            } else {
                e.returnValue = false;
            }
            var str = comment.slice(0, maxChar); //中文还能输入，所以只裁切规定的字数
            commentCon.value = str.trim(); //再重新赋值，并去除两边空格
        }
        //快捷键发送
        var keyCode = e.which || e.keyCode;
        if (e.ctrlKey && keyCode == 13) enterHandler();
    }

    // 发送的方法
    function enterHandler() {
        var comment = commentCon.value;
        if (comment) {
            var li = document.createElement("li");
            li.innerHTML =
                `<div class="comment-header">
                        <div class="nick">留言人：a123123</div>
                        <div class="time">${dateFormat("YY-MM-DD hh:mm")}</div>
                        <div class="comment">${comment}</div>
                    </div>`;


            showComment.prepend(li);
            commentCon.value = "";

            commentCon.disabled = true;
            commentSend.disabled = true;

            commentCon.onkeypress = commentCon.onkeyup = null;
            commentSend.onclick = null;

            var count = 0;
            commentTips.textContent = `距离下一次输入还剩${count}秒`;
            var timer = setInterval(function () {
                count--;
                commentTips.textContent = `距离下一次输入还剩${count}秒`;
                if (count <= 0) {
                    clearInterval(timer);
                    commentTips.textContent = `您还可以输入50个字`;

                    commentCon.disabled = false;
                    commentSend.disabled = false;

                    commentCon.onkeypress = commentCon.onkeyup = inputHandler;
                    commentSend.onclick = enterHandler;
                }
            }, 1000);
        }
    }


    function dateFormat(replace, date) {
        var date = day == undefined ? new Date() : new Date(date);
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        var hour = date.getHours();
        var minute = date.getMinutes();
        var second = date.getSeconds();
        return replace.replace("YY", year).replace("MM", month).replace("DD", day).replace("hh", hour).replace("mm", minute).replace("ss", second);
    }

    function parent(ele) {
        return ele.parentElement;
    }

    function prev(ele) {
        return ele.previousElementSibling || ele.previousSibling;
    }


</script>

</html>